Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

icon from window handle ?

706 views
Skip to first unread message

stei...@yahoo.com

unread,
Mar 22, 2006, 12:48:47 PM3/22/06
to
Hi all ! This is my first post I hope I am posting in the right place !

I am developing an application in .net with visual c# for windows
mobile 5.

I have the list of running programs and their handles. How can I get
their icon so I can populate an imagelist ?

Thanks !

Alex Feinman [MVP]

unread,
Mar 22, 2006, 3:46:58 PM3/22/06
to
You need to send WM_GETICON (0x007f) )message to the application main
window.
IntPtr hIcon = (IntPtr)SendMessage(hWnd, 0x007f, 0, 0);

[DllImport("Coredll")]
extern static int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);

Once you have hIcon, you can use Icon.FromHandle to get a .NET Icon object

<stei...@yahoo.com> wrote in message
news:1143049727....@j33g2000cwa.googlegroups.com...

stei...@yahoo.com

unread,
Mar 22, 2006, 9:09:24 PM3/22/06
to
Thank you very much for your reply. Unfortunately the code doesn't
work very well. For example I cannot get the icon of my own application
when the icon is set in the Project Properties. I can only get the icon
when I set it in the Form Properties. As a result I cannot get the icon
of all other applications running on my pocket pc :(

I wonder if it is better to find the executable that launched the
window and get the icon from there using SHGetFileInfo(), which seems
to work very well. But it seems to be dificult.

I would really appreciate any other suggestions. Thanks again !

Alex Feinman [MVP]

unread,
Mar 22, 2006, 9:18:58 PM3/22/06
to
You can get to the process ID from hWnd by calling GetWindowThreadProcessId.
From there you need to use toolhelp to enumerate processes and their modules
as shown here
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetcomp/html/ProcessManager.asp
Then you can use SHGetFileInfo to get the icon

<stei...@yahoo.com> wrote in message
news:1143079764....@j33g2000cwa.googlegroups.com...

stei...@yahoo.com

unread,
Mar 23, 2006, 3:45:03 PM3/23/06
to
Thank you very much for your guidance. I think I am very close to the
solution but I still have a problem.

I get the process ID using: GetWindowThreadProcessId();

In order to use GetModuleFileName() I need the process handle instead
of the ID.

So I use OpenProcess(), but it always returns 0

This is an example of my code: (handle is the window handle)


string path = new string(' ', 255);
IntPtr ThreadID =GetWindowThreadProcessId(handle, IntPtr.Zero);

IntPtr processHwnd = OpenProcess(0, false, (int)ThreadID);

GetModuleFileName(processHwnd.ToInt32(), path, path.Length);

Is it that OpenProcess() is used in the msdn example so it should be
supported. I don't understand what is wrong ?

stei...@yahoo.com

unread,
Mar 23, 2006, 4:00:52 PM3/23/06
to
I forgot to mention that I also tried:

[DllImport("coredll.dll")]
private static extern IntPtr OpenProcess(uint flags, bool
fInherit, int PID);
private const uint PROCESS_ALL_ACCESS = 0x1F0FFF;


IntPtr processHwnd = OpenProcess(PROCESS_ALL_ACCESS, false,
(int)ThreadID);

But the result is the same :( Any help would be greately appreciated.
Thanks !

stei...@yahoo.com

unread,
Mar 23, 2006, 6:32:00 PM3/23/06
to
oops it was my error ! It works great now !

Thanks for pointing me to the right direction !

0 new messages